<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" width="450px" :title="`${dialogProps.title}做法标签`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="dialogProps.isView"
      :model="dialogProps.row"
      :hide-required-asterisk="dialogProps.isView"
    >
      <el-form-item label="做法分组" prop="taskTypeId">
        <el-select class="w100" v-model="dialogProps.row!.taskTypeId" clearable>
          <el-option v-for="item in TaskType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="做法标签" prop="name">
        <el-input v-model="dialogProps.row!.name" placeholder="请填写" clearable></el-input>
      </el-form-item>
      <el-form-item label="做法变价" prop="addPriceWay">
        <el-select class="w100" v-model="dialogProps.row!.addPriceWay" clearable>
          <el-option v-for="item in FoodTaskPriceType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input-number class="w100" v-model="dialogProps.row!.price" placeholder="请输入" :controls="false" />
      </el-form-item>
      <el-form-item label="是否通用" prop="currencyFlag">
        <el-radio-group v-model="dialogProps.row!.currencyFlag">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false"> 取消 </el-button>
      <el-button v-show="!dialogProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="MethodDialog">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Goods, System } from "@/api/interface";
import { useSelectStore } from "@/stores/modules/dict";
import { getFoodtaskCategory } from "@/api/modules/goods";

const rules = reactive({
  taskTypeId: [{ required: true, message: "做法分组必选" }],
  name: [{ required: true, message: "做法标签必填" }]
});

interface DialogProps {
  title: string;
  isView: boolean;
  row: Partial<Goods.ParentFoodtaskList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}
const FoodTaskPriceType = ref<System.ResDictList[]>([]);
const TaskType = ref<System.ResDictList[]>([]);
const dialogVisible = ref(false);
const dialogProps = ref<DialogProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DialogProps) => {
  dialogProps.value = params;
  dialogVisible.value = true;
  useSelectStore()
    .getDic("FoodTaskPriceType")
    .then(data => {
      FoodTaskPriceType.value = data;
      if (dialogProps.value.title === "新增") {
        dialogProps.value.row.addPriceWay = FoodTaskPriceType.value[0].value;
      }
    });
  getFoodtaskCategory().then(res => {
    TaskType.value = res.data.list.map((im: any) => {
      return { value: im.id, label: im.name };
    });
    if (dialogProps.value.title === "新增") {
      dialogProps.value.row.currencyFlag = 1;
      dialogProps.value.row.taskTypeId = TaskType.value[0].value;
    }
  });
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await dialogProps.value.api!(dialogProps.value.row);
      ElMessage.success({ message: `${dialogProps.value.title}做法标签成功！` });
      dialogProps.value.getTableList!();
      dialogVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
